﻿using System;
using System.ComponentModel;
using System.Runtime.InteropServices;

namespace Gmantis.Controls.DataGrid
{
    [StructLayout(LayoutKind.Sequential), TypeConverter(typeof(DataGridScrollStepConverter))]
    public struct DataGridScrollStep
    {
        private double _value;
        private DataGridScrollStepUnitType _type;
        private static DataGridScrollStep _discrete;
        static DataGridScrollStep()
        {
            _discrete = new DataGridScrollStep(1.0, DataGridScrollStepUnitType.Discrete);
        }

        public DataGridScrollStep(double value)
            : this(value, DataGridScrollStepUnitType.Pixel)
        {
        }

        public DataGridScrollStep(double value, DataGridScrollStepUnitType type)
        {
            if ((double.IsNaN(value) || double.IsInfinity(value)) || (value < 0.0))
            {
                throw new IndexOutOfRangeException();
            }
            this._value = (type == DataGridScrollStepUnitType.Discrete) ? 1.0 : value;
            this._type = type;
        }

        public bool IsDiscrete
        {
            get
            {
                return (this._type == DataGridScrollStepUnitType.Discrete);
            }
        }
        public bool IsAbsolute
        {
            get
            {
                return (this._type == DataGridScrollStepUnitType.Pixel);
            }
        }
        public double Value
        {
            get
            {
                if (this._type != DataGridScrollStepUnitType.Discrete)
                {
                    return this._value;
                }
                return 1.0;
            }
        }
        public static DataGridScrollStep Discrete
        {
            get
            {
                return _discrete;
            }
        }
        public DataGridScrollStepUnitType StepUnitType
        {
            get
            {
                return this._type;
            }
        }
        public override string ToString()
        {
            switch (this._type)
            {
                case DataGridScrollStepUnitType.Discrete:
                    return "Discrete";
            }
            return this._value.ToString();
        }
    }
}
